package com.chencs.app.shop.service;

import com.chencs.app.model.mapper.AppModelMapper;
import com.chencs.app.shop.mapper.AppShopMapper;
import com.chencs.biz.Common;
import com.chencs.vo.biz.shop.NewShopModel;
import com.chencs.vo.biz.shop.ShopModel;
import com.chencs.vo.net.request.ShopRequest;
import com.chencs.vo.net.response.ShopInfoResponse;
import com.taobao.api.ApiException;
import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.TbkShopGetRequest;
import com.taobao.api.response.TbkShopGetResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by fangzehua on 2017/8/15.
 */
@Service
@Transactional
public class AppShopService {


    @Autowired
    private AppShopMapper appShopMapper;



    public List<ShopModel> getShopList(ShopRequest request){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("pageno",request.getPageNo()*request.getPageSize());
        map.put("pagesize",request.getPageSize());

        List<ShopModel> list  = appShopMapper.getShopList(map);
        for(ShopModel model:list){
            model.setProducts(appShopMapper.findShopProBySid(model.getId()));
        }

        return  list;
    }



    public ShopInfoResponse getShopInfo(String shopname){

        ShopInfoResponse shopInfoResponse = new ShopInfoResponse();
        TaobaoClient client = new DefaultTaobaoClient(Common.apiUrl+"taobao.tbk.shop.get", Common.appKey,Common.appSecre);
        TbkShopGetRequest req = new TbkShopGetRequest();
        req.setFields("user_id,shop_title,shop_type,seller_nick,pict_url,shop_url,is_tmall");
        req.setQ(shopname);
        req.setSort("commission_rate_des");
        req.setPageNo(1L);
        req.setPageSize(1L);
        TbkShopGetResponse rsp;
        try {
            rsp = client.execute(req);
          //  System.out.println(rsp.getBody());
            if(rsp!=null){
            ShopModel shopModel = new ShopModel();
                shopModel.setIcon(rsp.getResults().get(0).getPictUrl());
                shopModel.setName(rsp.getResults().get(0).getShopTitle());
                shopModel.setUrl(rsp.getResults().get(0).getShopUrl());
                shopModel.setType(rsp.getResults().get(0).getShopType().equals("B")?"1":"0");
                shopModel.setId(rsp.getResults().get(0).getUserId()+"");
                shopInfoResponse.setShop(shopModel);
            }

        } catch (ApiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return  null;
        }

        return  shopInfoResponse;
    }


    public List<NewShopModel> getShopByCid(ShopRequest request){
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("pageno",request.getPageNo()*request.getPageSize());
        map.put("pagesize",request.getPageSize());
        map.put("type",request.getCid());
        List<NewShopModel> list  = appShopMapper.getShopByCid(map);
        return  list;
    }
}
